package com.wjjun.customview.window;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.wjjun.customview.R;

/**
 * Created by 67535 on 2018/5/16.
 */
public abstract class BasePopupWindow extends PopupWindow implements View.OnClickListener {

    private Context context;
    protected View rootView;
    private TextView titleView;
    private View contentView;
    private View cancelView;
    private View confirmView;

    protected OnWindowListener onWindowListener;

    public BasePopupWindow(@NonNull Context context) {
        super(context);
        this.context = context;
        initViews();
        initContent();
        initData();
    }

    protected void initViews() {
        LayoutInflater inflater = LayoutInflater.from(context);
        rootView = inflater.inflate(getLayoutId(), null);
        setContentView(rootView);

        setAnimationStyle(R.style.popupWindowStyle);
        setBackgroundDrawable(new ColorDrawable(Color.parseColor("#80000000")));
        setOutsideTouchable(true);
        setTouchable(true);
        setFocusable(true);
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {

            }
        });

        titleView = findViewById(R.id.window_title);
        contentView = findViewById(R.id.window_content);
        cancelView = findViewById(R.id.window_cancel);
        confirmView = findViewById(R.id.window_confirm);

        if (cancelView != null) {
            cancelView.setOnClickListener(this);
        }
        if (confirmView != null) {
            confirmView.setOnClickListener(this);
        }
    }

    protected abstract int getLayoutId();

    protected void initContent() {

    }

    protected void initData() {

    }

    protected String getString(int resId) {
        if (context != null) {
            return context.getString(resId);
        }
        return "";
    }

    protected void showToast(int resId) {
        ToastUtils.showShort(resId);
    }

    public final <T extends View> T findViewById(int id) {
        if (id == 0) {
            return null;
        }
        return rootView.findViewById(id);
    }

    public void setTitle(int resId) {
        if (titleView != null) {
            titleView.setText(resId);
        }
    }


    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.window_cancel) {
            if (onWindowListener != null) {
                onWindowListener.onCancel();
            }
        } else if (v.getId() == R.id.window_confirm) {
            onConfirm();
        }
    }

    protected void onConfirm() {
        if (onWindowListener != null) {
            onWindowListener.onConfirm(getConfirmValue());
        }
    }

    protected Object getConfirmValue() {
        return null;
    }

    public OnWindowListener getOnWindowListener() {
        return onWindowListener;
    }

    public void setOnWindowListener(OnWindowListener onDialogListener) {
        this.onWindowListener = onDialogListener;
    }

    public interface OnWindowListener {
        void onCancel();

        void onConfirm(Object value);
    }

    public void show(Activity activity) {
        showAtLocation(activity.getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);
    }
}
